<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <title>postMessage8081-子窗口</title>
</head>

<body>
    <button onclick="send()">发送</button>
    <!--<script>
        function send() {
            //window.opener指的就是父窗口（打开本窗口的窗口）
            window.opener.postMessage("我是子窗口，向主窗口发送消息", "http://localhost:8080/html_review_code/跨域/postMessage-test1.html");
        }
        function receiveMessage(event) {
            if (event.origin !== "http://localhost:8080") return;
            console.log(event.data);
        }
        window.addEventListener("message", receiveMessage, false);
    </script>-->
    <script>
        function send() {
            window.opener.postMessage('子窗口：hello！', 'http://localhost:8080');
        }
        window.addEventListener('message', receiveMessage);
        function receiveMessage(event) {
            if (event.origin !== "http://localhost:8080") return;
            if (event.data.length > 3) {
                console.log(event.data)
                console.log('长度>3，进行回复')
                event.source.postMessage('子窗口：hello，父窗口发送长度大于3', event.origin);
            } else {
                console.log(event.data)
                console.log('长度<3，不进行回复')
            }

        }
    </script>
</body>

</html>